home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / msg / connectInitialize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.9 KB  |  233 lines

  1. /*
  2.  *   $RCSfile: connectInitialize.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:54 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #ifdef JUNK
  39. #ifdef defined(c_plusplus) & !defined(__cplusplus)
  40. #    define getservbyname _old_getservbyname
  41. #    include <netdb.h>
  42. #    undef getservbyname
  43.     extern servent    *getservbyname(char *, char *);
  44. #else
  45. #    include <netdb.h>
  46. #endif c_plusplus
  47. #endif JUNK
  48.  
  49. #include "sysdefs.h"
  50. #include <fcntl.h>
  51. #include "ess.h"
  52. #include "checking.h"
  53. #include "trace.h"
  54. #include "error.h"
  55. #include "list.h"
  56. #include "tid.h"
  57. #include "io.h"
  58. #include "lock.h"
  59. #include "object.h"
  60. #include "msgdefs.h"
  61. #include "thread.h"
  62. #include "semaphore.h"
  63. #include "link.h"
  64. #include "host.h"
  65. #include "bitvec.h"
  66. #include "msgvector.h"
  67. #include "msg_funcs.h"
  68. #include "util_funcs.h"
  69. #include "msg_globals.h"
  70.  
  71.  
  72. #if defined(__cplusplus) && !defined(hpux)
  73. #elif c_plusplus
  74. #elif hpux
  75. #else
  76. extern SERVENT    *getservbyname(char *, char *);
  77. #endif
  78.  
  79.  void
  80. connectInitialize ()
  81.  
  82. {
  83.     SOCKADDR            ConnectAddr; 
  84.     register LINK        *linkTcp, *linkUdp;
  85.     register SERVENT    *_servent=(SERVENT *)0;
  86.     int                    length;
  87.     int                    retryLimit = 20;
  88.     int                    retryCount;
  89.     int                    socketBufSize = 16*1024; /* 16K socket buffers */
  90.  
  91.     TRACE(TR_MSG, TR_LEVEL_1);
  92.  
  93.     /*
  94.      *    get the port on which to listen for client connect requests.
  95.      */
  96.     if ( (!ServePort) && 
  97.         ((_servent = getservbyname(ServeName, "tcp")) == NULL))    {
  98.  
  99.         SM_ERROR(TYPE_FATAL, errno);
  100.     }
  101.  
  102.     /*
  103.      *    initialize the address
  104.      */
  105.     ConnectAddr.sin_family = AF_INET;
  106.     ConnectAddr.sin_addr.s_addr = INADDR_ANY;
  107.     if(_servent)  {
  108.         ConnectAddr.sin_port = _servent->s_port;
  109.     } else  {
  110.         ConnectAddr.sin_port = ServePort;
  111.     }
  112.     TRPRINT(TR_MSG, TR_LEVEL_2, ("host inet addr:%s",
  113.               inet_ntoa(*(struct in_addr *)&ConnectAddr.sin_addr.s_addr)));
  114.  
  115.     /*
  116.      *    create the socket
  117.      */
  118.     if ((fdTcp = socket(AF_INET, SOCK_STREAM, 0)) < 0)    {
  119.  
  120.         SM_ERROR(TYPE_FATAL, errno);
  121.     }
  122.     TRPRINT(TR_MSG, TR_LEVEL_2, ("connection socket:%d", fdTcp));
  123.  
  124.     if ((fdUdp = socket(AF_INET, SOCK_DGRAM, 0)) < 0)    {
  125.  
  126.         SM_ERROR(TYPE_FATAL, errno);
  127.     }
  128.     TRPRINT(TR_MSG, TR_LEVEL_2, ("datagram socket:%d", fdUdp));
  129.  
  130.     /*
  131.       *    set socket to not delay sending data and enlarge the socket
  132.      *    buffer size
  133.      */
  134.     if (setSocketOptions(fdTcp, socketBufSize, socketBufSize) != esmNOERROR) {
  135.         SM_ERROR(TYPE_FATAL, esmINTERNAL);
  136.     }
  137.  
  138.     /*
  139.      *    set the socket to close on exec for disk processes
  140.      */
  141.     if (fcntl(fdTcp, F_SETFD, NULL) < 0)    {
  142.         SM_ERROR(TYPE_FATAL, errno);
  143.     }
  144.     if (fcntl(fdUdp, F_SETFD, NULL) < 0)    {
  145.         SM_ERROR(TYPE_FATAL, errno);
  146.     }
  147.  
  148.     /*
  149.      *    mark the link as used
  150.      */
  151.     linkTcp = &(Links[fdTcp]);
  152.     listRemove( &(linkTcp->list) );
  153.     linkTcp->linkClass = CL_CONNECT;
  154.  
  155.     linkUdp = &(Links[fdUdp]);
  156.     listRemove( &(linkUdp->list) );
  157.     linkUdp->linkClass = CL_DGRAM;
  158.  
  159.     /*
  160.      *    get the size of the structure
  161.      */
  162.     length = sizeof(ConnectAddr);
  163.  
  164.     /*
  165.      *  bind the address to the socket.  This may fail because
  166.      *  brain-damaged OS's don't like to free sockets right away.
  167.      *  So, try a few times.
  168.      */
  169.     for (retryCount = 0; retryCount < retryLimit; retryCount++) {
  170.  
  171.         if (bind(fdTcp, (struct sockaddr *) &ConnectAddr, sizeof(ConnectAddr)) < 0)
  172.     {
  173.  
  174.             if (errno == EADDRINUSE && (retryCount+1 < retryLimit)) {
  175.                 SM_ERROR(TYPE_LOG, errno);
  176.                 fprintf(stderr, "Retrying in 10 seconds ...\n");
  177.                 sleep(10 /*seconds*/);
  178.             } else {
  179.                 fprintf(stderr,  "Port %d/tcp is in use by another process.\n",
  180.                      ntohs(ConnectAddr.sin_port));
  181.  
  182.                 SM_ERROR(TYPE_STOP, errno);
  183.             }
  184.         } else {
  185.             /* 
  186.              * The Udp socket really SHOULD be free if the tcp one is.
  187.              * We assume that noone is going to grab this 
  188.              * unofficially "well-known" port.
  189.              */
  190.             if (bind(fdUdp, (struct sockaddr *) &ConnectAddr, 
  191.                 sizeof(ConnectAddr)) < 0) {
  192.  
  193.                 fprintf(stderr,  "Port %d/udp is in use by another process.\n",
  194.                      ntohs(ConnectAddr.sin_port));
  195.  
  196.                 SM_ERROR(TYPE_STOP, errno);
  197.             }
  198.             /* success to exit retry loop */
  199.             break;
  200.         }
  201.  
  202.     }
  203.     /*
  204.      *    get the name of the socket
  205.      */
  206.     if (getsockname(fdTcp, (struct sockaddr *) &ConnectAddr, &length))    {
  207.  
  208.         SM_ERROR(TYPE_FATAL, errno);
  209.     }
  210.     TRPRINT(TR_MSG, TR_LEVEL_2, ("socket has port:%d", ntohs(ConnectAddr.sin_port)));
  211.  
  212.     /*
  213.      *    set the receive information
  214.      */
  215.     setLink(linkTcp);
  216.     setLink(linkUdp);
  217.  
  218.     /*
  219.      *    set to accept connections
  220.      */
  221.     if (listen(fdTcp, 5) < 0)    {
  222.  
  223.         SM_ERROR(TYPE_STOP, errno);
  224.     }
  225.     TRPRINT(TR_MSG, TR_LEVEL_2, ("listening on link:%d", fdTcp));
  226.  
  227.     /*
  228.      *    set the status of the link to listen
  229.      */
  230.     linkTcp->flags = LINK_LISTEN;
  231.     linkUdp->flags = LINK_LISTEN; /* poor word for datagram use */
  232. }
  233.